package fun.qu_an.minecraft.mini_games_helper.client.mixin.recipebook;

import fun.qu_an.minecraft.mini_games_helper.client.handle.ForceOptionHelperVanilla;
import net.minecraft.client.MinecraftClient;
import net.minecraft.client.gui.Drawable;
import net.minecraft.client.gui.Element;
import net.minecraft.client.gui.Selectable;
import net.minecraft.client.gui.screen.Screen;
import net.minecraft.client.gui.screen.recipebook.RecipeBookWidget;
import net.minecraft.client.gui.widget.TexturedButtonWidget;
import org.jetbrains.annotations.Nullable;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.Shadow;
import org.spongepowered.asm.mixin.Unique;
import org.spongepowered.asm.mixin.injection.At;
import org.spongepowered.asm.mixin.injection.Inject;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfoReturnable;

@Mixin(Screen.class)
public class MixinScreen {
	@Shadow @Nullable protected MinecraftClient client;
	@Unique
	private static final String RecipeButtonTexturePath = "textures/gui/recipe_button.png";

	@Inject(method = "addDrawableChild(Lnet/minecraft/client/gui/Element;)Lnet/minecraft/client/gui/Element;",
		at = @At("HEAD"), cancellable = true)
	protected <T extends Element & Drawable & Selectable> void addDrawableChild(T child, CallbackInfoReturnable<T> info) {
		if (client != null
			&& client.interactionManager != null
			&& child instanceof TexturedButtonWidget widget
			&& RecipeBookWidget.BUTTON_TEXTURES.equals(widget.textures)
			&& ForceOptionHelperVanilla.isDisableRecipeBook(client.interactionManager.getCurrentGameMode())) {
			info.setReturnValue(child);
		}
	}
}
